<?php
declare (strict_types=1);
/**
 * @Description:
 * @Author: Chern
 * @Date: 2023/7/10
 * @Time: 17:11
 */

namespace app\model;

use think\Model;

class UserOrganization extends Model
{
    /**
     * 系统架构表
     * 查询现有系统架构
     * @return UserOrganization[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getStructure()
    {
        return $this->select();
    }
    public function getStructureIdPersonIdModel()
    {
        return $this->field('id,parent_id')->select ();
    }
    public function getStructureArr()
    {
        return $this->select()->toArray ();
    }

    public function getStructureArrDataModel()
    {
        return $this->select()->toArray ();
    }


    /**
     * 修改系统架构名称
     * @param $param
     */

    public function updateStructure($param)
    {
        return self::update($param);
    }

    /**
     * 添加系统架构
     * @param $data
     * @return UserOrganization|Model
     */

    public function insertStructure($data)
    {
        return self::create($data);
    }

    /**
     * 删除系统架构信息
     * @param $id
     * @return bool
     */
    public function deleteStructure($id)
    {
        return self::destroy($id);
    }

    /**
     * @param $id
     * @return mixed
     */
    public function getStructureName($id)
    {
        return $this->where(['id'=>$id])->value('title');
    }

    /**
     * 根据用户id查询用户所属公司及部门信息
     * @param $uid
     * @return UserOrganization
     */
    public function getUserDepartmentInfoModel($uid)
    {
        return UserOrganization::find($uid);
    }

    /**
     * 查询部门id范围
     * @param $idIn
     * @return \app\model\UserOrganization[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOrgInNameModel($idIn)
    {
        return $this->whereIn ('id',$idIn)->select()->toArray ();
    }

    /**
     * 查询id以及id的子集
     * @param $id
     * @return \app\model\UserOrganization[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOidParentDataModel($id)
    {
        return $this->whereRaw ('id='.$id.' OR parent_id='.$id)->select()->toArray ();
    }


    /**
     * 获取所有总公司合集
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCompanyTypeData($parId=0)
    {
        return $this->where (['parent_id'=>$parId])->select()->toArray ();
    }

    public function getDepartments($department_id, $arr = [])
    {
        $info = self::fetchSql(false)->where('id', $department_id)->field("parent_id")->find();
        if ($info && $info['parent_id']) {
            array_unshift($arr, $info['parent_id']);
            return $this->getDepartments($info['parent_id'], $arr);
        }
        return $arr;
    }

    public function getDataRangeDepartmentList($list = [], $p_id = '0', $isFirstTime = true)
    {
        static $arr = [];
        if ($isFirstTime) {
            $arr = [];
        }
        if (!$list) {
            $list = self::fetchSql(false)->select()->toArray();
        }
        foreach ($list as $key => $val) {
            if ($val['parent_id'] == $p_id) {
                $arr[] = $val['id'];
                $this->getDataRangeDepartmentList($list, $val['id'], false);
            }
        }
        return $arr;
    }

    /**
     * 获取总公司id
     * @param $id
     * @return array|null
     */
    public function getZgsId($id=0)
    {
        return $this->where (['parent_id'=>$id])->value ('id');
    }

    /**
     * 通过部门名称获取部门ID
     * @param $name
     * @return array|string
     */
    public function getOrgIdModel($name)
    {
        return $this->where(['title'=>$name])->value ('id');
    }

}